﻿@using Pay.Core;
@{
    Layout = null;
    var img_url = ViewBag.img_url;
    var bankCard = ViewBag.BankCard as api_merchant_bank_card_dto;
    int bankCardStatus = 3;
    if (bankCard == null) {
        bankCardStatus = 0;
    } else if (bankCard.status == EMerchantBankCardStatus.ToAudit) {
        bankCardStatus = 1;
    } else if (bankCard.status == EMerchantBankCardStatus.AuditFail) {
        bankCardStatus = 4;
    }
}
@model app_consume_product_dto
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>确认兑换</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0,maximum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="~/Content/css/base.css" />
    <link rel="stylesheet" href="~/Content/css/tuoken.css" />
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="~/Content/js/base.js"></script>
    <link href="~/Content/js/layer/1.9.3/layui.css" rel="stylesheet" />
    <script src="~/Content/js/layer/1.9.3/layer.js"></script>
</head>

<body>
    @*<div class="header fn-shadow">
            <a href="javascript:history.go(-1);">
                <span class="back"></span>
            </a>
            <div class="hdtitle">
                选择地址
            </div>
        </div>*@

    <div class="property exchange">
        <div class="choseAddress">
            @if (ViewBag.address != null) {
                var address = (shipping_address_dto)ViewBag.address;
                <div class="item adrress" data-id="@address.id" onclick="location.href='/user/myadress?returnUrl=@Request.Url'">
                    <i class="icon-address fn-left"></i>
                    <div class="hidden">
                        <div class="top clear">
                            <span class="fn-right">@address.phone</span>
                            <span>收货人：@address.ship_to</span>
                        </div>
                        <p class="col-gray1">收货地址：@(address.region_name + address.address)</p>
                    </div>
                </div>
            } else {
                <div class="addAddress adrress" data-id="0">
                    <a href="/user/adress?id=0&returnUrl=@Request.Url" class="col-yellow fn14 mb10">+创建新的收货地址</a>
                </div>
            }

        </div>
        <div class="for_record">
            <div class="conversion_dh">
                <div class="img">
                    <img src="@(img_url+Model.img_url)" alt="@Model.title">
                </div>
                <div class="rg">
                    <div class="text">
                        <p class="fn14">
                            @Model.title
                        </p>
                        <p class="fn12">
                            @Model.detail
                        </p>
                    </div>
                    <p class="fn12 col-gray1">兑换红砖：<strong class="col-yellow">@Model.red_drill</strong></p>
                </div>
            </div>
        </div>
        <a href="javascript:void(0)" class="settingbtn ">确认兑换</a>
        <input type="hidden" value="@Model.id" name="id" id="productId" data-red="@Model.red_drill" />
    </div>

    <script type="text/javascript">
        $(function () {
            var browser = {
                versions: function () {
                    var u = navigator.userAgent, app = navigator.appVersion;
                    return {
                        //移动终端浏览器版本信息
                        trident: u.indexOf('Trident') > -1, //IE内核
                        presto: u.indexOf('Presto') > -1, //opera内核
                        webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                        gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                        mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                        ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                        android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                        iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                        iPad: u.indexOf('iPad') > -1, //是否iPad
                        webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
                        WeiXin: u.match(/MicroMessenger/i) == "MicroMessenger" //是否为微信客户端
                    };
                }(),
                language: (navigator.browserLanguage || navigator.language).toLowerCase()
            };

            var flag = false;
            $(".settingbtn").click(function () {
                var addressId = $('.adrress').attr("data-id");
                if (addressId == 0) { layer.msg("请选择收货地址"); return; }

                var bankCardStatus = Number('@bankCardStatus');
                if (bankCardStatus == 0 || bankCardStatus == 1 || bankCardStatus == 4) {
                    var tip = '';
                    flag = true;
                    if (bankCardStatus == 0)
                        tip = '您未绑定银行卡，不能进行兑换，请到“我的->账号设置->银行卡管理”中进行绑定';
                    else if (bankCardStatus == 1)
                        tip = '您的银行卡信息正在审核中，不能进行兑换，请耐心等待';
                    else if (bankCardStatus == 4)
                        tip = '您的银行卡信息审核未通过，不能进行兑换，请到“我的->账号设置->银行卡管理”中重新提交';
                    layer.msg(tip, {
                        time: 0 //不自动关闭
                        , btn: ['确定', '取消']
                        , yes: function (index) {
                            layer.close(index);
                            if (bankCardStatus == 0 || bankCardStatus == 4) {
                                try {
                                    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                                        window.webkit.messageHandlers.toBindBankCard.postMessage('');
                                    }
                                    else if (/(Android)/i.test(navigator.userAgent)) {
                                        androidfuntion.toBindBankCard();
                                    }
                                } catch (e) {
                                    location.href = '@Url.Action("MyBankCard", "MerchantBankCard")';
                                }
                                
                            }
                        },
                        btn2: function () {
                        }
                    });
                }

                if (flag) return;
                flag = true;
                layer.msg('你确定要兑换么？', {
                    time: 0 //不自动关闭
                    , btn: ['确定', '取消']
                    , yes: function (index) {
                        layer.close(index);
                        $.post("/Consumption/Convert", { id: $("#productId").val(), reddrill: $("#productId").attr("data-red"), shipAddressId: addressId }, function (result) {
                            if (result.result_code == 1000) {
                                layer.msg("兑换成功");
                                location.href = "/Consumption/ExchangeRecord?fromSource=confirmExchange"
                            } else {
                                layer.msg(result.result_msg);
                            }
                            flag = false;
                        });
                    },
                    btn2: function () {
                        flag = false;
                    }
                });

            });
        })
    </script>
</body>

</html>